﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace EMath {
	/// <summary>
	/// Interaction logic for SplashScreen.xaml
	/// </summary>
	public partial class SplashScreen : Window {
		private BackgroundWorker BW_Loader;

		public SplashScreen( ) {
			InitializeComponent( );

			this.BW_Loader = new System.ComponentModel.BackgroundWorker( );
			this.BW_Loader.WorkerReportsProgress = true;
			this.BW_Loader.DoWork += new DoWorkEventHandler(BW_Loader_DoWork);
			this.BW_Loader.ProgressChanged += new ProgressChangedEventHandler(BW_Loader_ProgressChanged);
			this.BW_Loader.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BW_Loader_RunWorkerCompleted);

			this.lApplicationTitle.Content = AppAssembly.Product;
			this.lVersion.Content = String.Format(lVersion.Content.ToString( ), AppAssembly.Version);
			this.lVersionB.Content = String.Format(lVersionB.Content.ToString( ), AppAssembly.Version);
			this.lCopyright.Content = AppAssembly.Copyright + " by " + AppAssembly.Company;

			this.BW_Loader.RunWorkerAsync( );
		}

		private delegate void dtext(string x);
		private void text(string x) {
			if (!this.Dispatcher.CheckAccess( )) {
				this.Dispatcher.BeginInvoke(new dtext(text), x);
			} else {
				if (x != "") {
					if (this.lLoaderT.Content.ToString( ) != x) {
						this.lLoaderT.Content = x;
					}
				} else {
					this.lLoaderT.Content = "";
				}
			}
		}

		private void BW_Loader_DoWork(object sender, DoWorkEventArgs e) {
			// Inicializing
			this.BW_Loader.ReportProgress(0); //2 - Inicializing
			text("Inicialize");

			Dispatcher.Invoke(
				new Action(( ) => {
					App.MainWindow = new EMath.MainWindow( );
					Application.Current.MainWindow = App.MainWindow;
				})
			);

			Thread.Sleep(500);

			// Locales
			this.BW_Loader.ReportProgress(2); //6  - Searching Locales
			text("Searching Locales");

			this.BW_Loader.ReportProgress(8); //8  - Testing Locales
			text("Testing Locales");

			this.BW_Loader.ReportProgress(16); //5 - Loading Locales
			text("Loading Locales");


			// Themes
			this.BW_Loader.ReportProgress(21); //5 - Searching Themes
			text("Searching Themes");

			this.BW_Loader.ReportProgress(26); //4 - Loading Themes
			text("Loading Themes");


			// Modules
			this.BW_Loader.ReportProgress(30); //5 - Searching Modules
			text("Searching Modules");

			this.BW_Loader.ReportProgress(35); //5 - Searching Locales for Modules
			text("Searching Locales for Modules");

			this.BW_Loader.ReportProgress(40); //10 - Testing Modules
			text("Testing Modules");

			this.BW_Loader.ReportProgress(50); //10 - Testing Locales for Modules
			text("Testing Locales for Modules");

			this.BW_Loader.ReportProgress(60); //3 - Loading Modules
			text("Loading Modules");

			Dispatcher.Invoke(
				new Action(( ) => {
					//App.ListUC.Add("Planimetrics", new Controls.Arithmetic.Planimetrics( ));
					App.ListUC.Add("PrimeNumbers", new Controls.Arithmetic.PrimeNumbers( ));
					App.ListUC.Add("Divisibility", new Controls.Arithmetic.Divisibility( ));
					App.ListUC.Add("NumeralSystems", new Controls.Arithmetic.NumeralSystems( ));
					App.ListUC.Add("Sets", new Controls.Arithmetic.Sets( ));

					App.ListUC.Add("FieldOfSets", new Controls.Algebra.FieldOfSets( ));
					App.ListUC.Add("MathematicalInduction", new Controls.Algebra.MathematicalInduction( ));
					App.ListUC.Add("Combinatorics", new Controls.Algebra.Combinatorics( ));
					App.ListUC.Add("MathematicalLogic", new Controls.Algebra.MathematicalLogic( ));
					App.ListUC.Add("Matrix", new Controls.Algebra.Matrix( ));
					App.ListUC.Add("ProbabilityTheory", new Controls.Algebra.ProbabilityTheory( ));
					App.ListUC.Add("Relations", new Controls.Algebra.Relations( ));
					App.ListUC.Add("AlgebraicEquation", new Controls.Algebra.AlgebraicEquation( ));
					App.ListUC.Add("Formula", new Controls.Algebra.Formula( ));
					App.ListUC.Add("DeMorgan", new Controls.Algebra.Theorems.DeMorgan( ));

					App.ListUC.Add("AnalyticGeometry", new EMath.Controls.Geometry.AnalyticGeometry( ));
					App.ListUC.Add("Planimetrics", new EMath.Controls.Geometry.Planimetrics( ));
					App.ListUC.Add("Stereometrics", new EMath.Controls.Geometry.Stereometrics( ));
					App.ListUC.Add("Trigonometry", new EMath.Controls.Geometry.Trigonometry( ));
					App.ListUC.Add("Pitagoras", new EMath.Controls.Geometry.Theorems.Pitagoras( ));
					App.ListUC.Add("Tales", new EMath.Controls.Geometry.Theorems.Tales( ));

					App.ListUC.Add("NumericalSequences", new EMath.Controls.Analysis.NumericalSequences( ));
					App.ListUC.Add("Statistics", new EMath.Controls.Analysis.Statistics( ));
					App.ListUC.Add("Functions", new EMath.Controls.Analysis.Functions( ));
					App.ListUC.Add("LimitsAndContinuityOfFunctions", new EMath.Controls.Analysis.LimitsAndContinuityOfFunctions( ));
					App.ListUC.Add("Calculus", new EMath.Controls.Analysis.Calculus( ));
				})
			);
			//App.ListUC.Add("Planimetrics", new EMath.Controls.Geometry.Planimetrics( ));

			this.BW_Loader.ReportProgress(63); //2 - Loading Locales for Modules
			text("Loading Locales for Modules");


			// Plugins
			this.BW_Loader.ReportProgress(65); //2 - Searching Plugins
			text("Searching Plugins");

			this.BW_Loader.ReportProgress(66); //2 - Searching Locales for Plugins
			text("Searching Locales for Plugins");

			this.BW_Loader.ReportProgress(68); //2 - Testing Plugins
			text("Testing Plugins");

			this.BW_Loader.ReportProgress(69); //1 - Testing Locales for Plugins
			text("Testing Locales for Plugins");

			this.BW_Loader.ReportProgress(70); //2 - Loading Plugins
			text("Loading Plugins");

			this.BW_Loader.ReportProgress(72); //2 - Loading Locales for Plugins
			text("Loading Locales for Plugins");


			// Styles
			this.BW_Loader.ReportProgress(74); //2 - Searching Styles
			text("Searching Styles");

			this.BW_Loader.ReportProgress(76); //2 - Testing Styles
			text("Testing Styles");

			this.BW_Loader.ReportProgress(78); //2 - Loading Styles
			text("Loading Styles");


			// Building
			this.BW_Loader.ReportProgress(80); //10 - Building
			text("Building");


			// Load Settings
			this.BW_Loader.ReportProgress(95); //3 - Loading Settings
			text("Loading Settings");


			BW_Loader.ReportProgress(100);
			Thread.Sleep(250);
		}

		private void BW_Loader_ProgressChanged(object sender, ProgressChangedEventArgs e) {
			this.PB_Loader.Value = e.ProgressPercentage;
			this.lLoaderP.Content = "- " + e.ProgressPercentage + "%";
		}

		private void BW_Loader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
			this.Close( );
		}
	}
}